package com.telit.android.controller;

import com.telit.android.dto.AppTrackDTO;
import com.telit.android.entity.AppPatrolTrack;
import com.telit.android.service.IAppPatrolTrackService;
import com.telit.common.entity.DataReturn;
import com.telit.common.entity.PageData;
import com.telit.common.enums.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @Auther amy
 * @Describe 巡护轨迹控制层
 */
@RestController
@RequestMapping("/app-patrol-track")
@Api(tags = "巡护模块", description = "/app-patrol-track")
public class AppPatrolTrackController {

    @Autowired
    private IAppPatrolTrackService appPatrolTrackService;

    @GetMapping(value = "/list/{protectorId}", produces = "application/json;charset=UTF-8")
    @ApiOperation("单个用户巡护信息无分页查询")
    public DataReturn listByProtectorId(@PathVariable("protectorId") Long protectorId, AppPatrolTrack appPatrolTrack) {
        appPatrolTrack.setProtectorId(protectorId);
        return DataReturn.ok(appPatrolTrackService.listByProtectorId(appPatrolTrack));
    }

    @PostMapping(value = "/add", produces = "application/json;charset=UTF-8")
    @ApiOperation("app端巡护轨迹添加")
    public DataReturn add(@Validated @RequestBody AppPatrolTrack appPatrolTrack) {
        if (appPatrolTrackService.add(appPatrolTrack) == 0) {
            return DataReturn.failure(ResultCode.OPERATE_ERROR.getCode(), "巡护轨迹上传失败");
        }
        return DataReturn.ok();
    }

    @GetMapping(value = "/queryPatrolTrackRecord", produces = "application/json;charset=UTF-8")
    @ApiOperation("护林员巡护 - 巡护记录")
    public DataReturn<PageData<List<AppPatrolTrack>>> queryPatrolTrackRecord(@RequestParam("current") Integer current,
                                                                             @RequestParam("size") Integer size,
                                                                             @RequestParam(required = false) Long protectorId
            , @RequestParam(required = false) String startTime, @RequestParam(required = false) String endTime) {
        PageData<List<AppPatrolTrack>> data = appPatrolTrackService.queryPatrolTrackRecord(current, size, protectorId,
                startTime, endTime);
        return DataReturn.ok(data);
    }

    @GetMapping(value = "/queryPatrolTrackRecordForRealName", produces = "application/json;charset=UTF-8")
    @ApiOperation("护林员巡护 - 巡护记录根据输入用户模糊查询")
    public DataReturn queryPatrolTrackRecordForInputRealName(@RequestParam("current") Integer current,
                                                             @RequestParam("size") Integer size,
                                                             @RequestParam(required = false) String districtCode,
                                                             @RequestParam(required = false) String realName,
                                                             @RequestParam(required = false) String startTime,
                                                             @RequestParam(required = false) String endTime) {
        PageData<Object> data = appPatrolTrackService.queryPatrolTrackRecordForRealName(current, size, districtCode,
                realName, startTime, endTime);
        return DataReturn.ok(data);
    }

    @GetMapping(value = "/queryDetailById", produces = "application/json;charset=UTF-8")
    @ApiOperation("护林员巡护 - 巡护详情")
    public DataReturn queryDetailById(@RequestParam("subTaskId") Long subTaskId,@RequestParam(required = false) Long protectorId) {
        AppTrackDTO data = appPatrolTrackService.queryDetailById(subTaskId, protectorId);
        return DataReturn.ok(data);
    }

}
